package com.xiumiing.net.base.net.observer;

import android.app.Activity;
import android.util.Log;


import com.blankj.utilcode.util.LogUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.xiumiing.net.base.net.bean.ResultEntity;
import com.xiumiing.net.base.net.regular.HttpCallBackRegular;
import com.xiumiing.net.base.net.util.DuckHttpException;
import com.xiumiing.net.base.net.util.HttpErrorPrompt;
import com.xiumiing.net.base.net.util.HttpUtils;
import com.xiumiing.net.base.uitls.ActivityManager;
import com.xiumiing.net.base.uitls.EmptyUtils;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import static com.qmuiteam.qmui.widget.dialog.QMUITipDialog.Builder.ICON_TYPE_LOADING;
import static com.xiumiing.net.base.net.RequestStatus.FAILED_CONNECTION;
import static com.xiumiing.net.base.net.RequestStatus.FAILED_RESULT;
import static com.xiumiing.net.base.net.RequestStatus.SUCCESS;

/**
 * @author Created by Jeff
 * on 2019/6/19.
 * des:网络回调
 */
public class NetObserver<T> implements Observer<ResultEntity<T>>, HttpCallBackRegular<T> {

    private boolean isLoadingDialog = false;

    public NetObserver(boolean isLoadingDialog) {
        this.isLoadingDialog = isLoadingDialog;
    }

    public NetObserver() {
    }

    private QMUITipDialog mTipDialog;

    /**
     * 显示数据加载
     */
    protected void showLoadingDialog(@QMUITipDialog.Builder.IconType int iconType, CharSequence tipWord) {
        Activity activity = ActivityManager.getAppInstance().currentActivity();
        dismissLoadingDialog();
        if (EmptyUtils.isNotEmpty(activity)) {
            mTipDialog = new QMUITipDialog.Builder(activity)
                    .setIconType(iconType)
                    .setTipWord(tipWord)
                    .create();
            mTipDialog.setCanceledOnTouchOutside(true);
        }
        if (EmptyUtils.isNotEmpty(mTipDialog)) {
            mTipDialog.show();
        }
    }

    /**
     * dismiss加载
     */
    protected void dismissLoadingDialog() {
        if (EmptyUtils.isNotEmpty(mTipDialog)) {
            mTipDialog.dismiss();
        }
    }

    @Override
    public void onNext(ResultEntity<T> result) {
        if (EmptyUtils.isEmpty(result)) {
            result = new ResultEntity<>();
            result.setCode(HttpErrorPrompt.FAILED_RESULT);
            result.setMsg("数据为空");
        }
        T t = result.getData();
        if (!result.isSuccess()) {
            onResultError(result.getCode(), result.getMsg());
            onFail(result.getCode(), result.getMsg());
            onComplete(FAILED_RESULT, null);
        } else {
            onSuccess(t);
            onComplete(SUCCESS, t);
        }
    }


    @Override
    public void onError(Throwable e) {
        LogUtils.e(NetObserver.class, Log.getStackTraceString(e));
        DuckHttpException exception = HttpUtils.handleNetThrowable(e);
        onHttpError(exception.getErrorCode(), exception.getErrorMsg());
        onFail(exception.getErrorCode(), exception.getErrorMsg());
        onComplete(FAILED_CONNECTION, null);
        if (isLoadingDialog) {
            dismissLoadingDialog();
        }
    }

    private void onFail(int errorCode, String errorMsg) {
        onError(errorCode, errorMsg);
        HttpErrorPrompt.errorPrompt(errorCode, errorMsg);
    }

    @Override
    public void onComplete() {
        if (isLoadingDialog) {
            dismissLoadingDialog();
        }
    }

    @Override
    public void onSubscribe(Disposable d) {
        if (isLoadingDialog) {
            showLoadingDialog(ICON_TYPE_LOADING, null);
        }
        onStart();
    }

}
